//
//  BaseManager.m
//  YunboPro
//
//  Created by leo on 9/22/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "BaseManager.h"
#import "QOauthKey.h"
#import "QWeiboRequest.h"
#import "AdapterHelp.h"
#import "NSURL+QAdditions.h"

#define AppKey			@"appKey"
#define AppSecret		@"appSecret"
#define AppTokenKey		@"tokenKey"
#define AppTokenSecret	@"tokenSecret"
static BaseManager *baseManager;
@implementation BaseManager

@synthesize appKey;
@synthesize appSecret;
@synthesize tokenKey;
@synthesize tokenSecret;
@synthesize verifier;
@synthesize response;
@synthesize VERIFY_URL;
@synthesize acces_token_url;
@synthesize request_token_url;
@synthesize requesttokenKey;
@synthesize requesttokenSecret;

#pragma ManagerDegelate

-(void)dealloc
{
    [appKey release];
    [appSecret release];
    [tokenKey release];
    [tokenSecret release];
    [verifier release];
    [response release];
    [response release];
    [VERIFY_URL release];
    [acces_token_url release];
    [request_token_url release];
    [requesttokenKey release];
    [requesttokenSecret release];
    [super dealloc];
    
}
- (void)parseRequestTokenKeyWithResponse:(NSString *)aResponse
{
	
	NSDictionary *params = [NSURL parseURLQueryString:aResponse];
	self.requesttokenKey = [params objectForKey:@"oauth_token"];
	self.requesttokenSecret = [params objectForKey:@"oauth_token_secret"];
	//self.userid = [params objectForKey:@"userid"];
}

- (void)parseTokenKeyWithResponse:(NSString *)aResponse
{
	
	NSDictionary *params = [NSURL parseURLQueryString:aResponse];
	self.tokenKey = [params objectForKey:@"oauth_token"];
	self.tokenSecret = [params objectForKey:@"oauth_token_secret"];
	//self.userid = [params objectForKey:@"userid"];
}

- (void)saveDefaultKey {
	
	[[NSUserDefaults standardUserDefaults] setValue:self.appKey forKey:AppKey];
	[[NSUserDefaults standardUserDefaults] setValue:self.appSecret forKey:AppSecret];
	[[NSUserDefaults standardUserDefaults] setValue:self.tokenKey forKey:AppTokenKey];
	[[NSUserDefaults standardUserDefaults] setValue:self.tokenSecret forKey:AppTokenSecret];
	[[NSUserDefaults standardUserDefaults] synchronize];
}


- (void)loadDefaultKey
{
    self.appKey = [[NSUserDefaults standardUserDefaults] valueForKey:AppKey];
	self.appSecret = [[NSUserDefaults standardUserDefaults] valueForKey:AppSecret];
	self.tokenKey = [[NSUserDefaults standardUserDefaults] valueForKey:AppTokenKey];
	self.tokenSecret = [[NSUserDefaults standardUserDefaults] valueForKey:AppTokenSecret];
    
}

-(id)initWithDefault
{
    self =[super init];
    return self;
}
+(id) instance
{
    baseManager =[[BaseManager alloc]initWithDefault];
    return baseManager;
}

- (NSString *)getRequestToken
{
    NSString *url=self.request_token_url;
   	
	QOauthKey *oauthKey = [self getOauthKey];
	oauthKey.callbackUrl = @"http://www.qq.com";//for example
	
	QWeiboRequest *request = [[QWeiboRequest alloc] init];
	NSString *retString = [request syncRequestWithUrl:url httpMethod:@"GET" oauthKey:oauthKey parameters:nil files:nil];
	NSLog(@"getToken-------------------%@",retString);
	[request release];
	return retString;
}

- (NSString *)getAccessTokenWithVerify:(NSString *)aVerify{
    NSString *url = self.acces_token_url;
	QOauthKey *oauthKey = [self getOauthKey];
    oauthKey.tokenKey = self.requesttokenKey;
    oauthKey.tokenSecret = self.requesttokenSecret;
	oauthKey.verify = aVerify;
  	QWeiboRequest *request = [[QWeiboRequest alloc] init];
	NSString *retString = [request syncRequestWithUrl:url httpMethod:@"GET" oauthKey:oauthKey parameters:nil files:nil];
   	[request  release];
	return retString;
}

- (BOOL) isLogin
{
    Boolean flag = NO;
     if (![self.appKey isEqualToString:@""] && ![self.appSecret isEqualToString:@""]) {
        
        if (self.tokenKey && ![self.tokenKey isEqualToString:@""] && 
            self.tokenSecret && ![self.tokenSecret isEqualToString:@""]) 
        {
            flag = YES;
            
        } else {
            // exit
             flag = NO;
            
        }
        
    }
    return flag;
    
}




- (NSURLRequest*) getURLRequest
{
    NSString *retString = [self getRequestToken ];
    [self parseRequestTokenKeyWithResponse:retString];
    NSString *url = [NSString stringWithFormat:@"%@%@", self.VERIFY_URL, self.requesttokenKey];
    NSURL *requestUrl = [NSURL URLWithString:url];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:requestUrl];
    return request;
}



//Verifyrequest




-(BOOL)VerifyRequest:(NSURLRequest *)request{
    NSString *query = [[request URL] query];
   
    AdapterHelp *adapterHelp = [[AdapterHelp alloc]init];
    
    NSString *verifier1 = [adapterHelp valueForKey:@"oauth_verifier" ofQuery:query];
    [adapterHelp release];
	if (nil!=verifier1 && ![verifier1 isEqualToString:@""]) {
        
		NSString *retString = [self getAccessTokenWithVerify:verifier1];
                                                  
        
		[self parseTokenKeyWithResponse:retString];
		[self saveDefaultKey];
        return NO;
	}
	
	return YES;
}

-(QOauthKey*)getOauthKey
{
    QOauthKey *oauthKey = [[QOauthKey alloc] init];
    oauthKey.consumerKey = self.appKey;
	oauthKey.consumerSecret = self.appSecret;
	oauthKey.tokenKey = self.tokenKey;
	oauthKey.tokenSecret= self.tokenSecret;
    [oauthKey autorelease];
    return oauthKey;
	
}
// Get MSG


- (NSArray *)getHomeMsgWithPageFlage:(PageFlag)aPageFlag 
                             nReqNum:(NSInteger)aReqNum
{
    return nil;
}

@end
